import { Component, OnInit, ViewChild } from '@angular/core';
import { MemberService } from '../member.server';
import { Router } from '@angular/router';
import { LayoutRourteStatus } from 'src/app/model/status';
import { MessageService, HUAN_ZHE_A, HUAN_ZHE_C, PHONE } from 'src/app/core/service';
import { FormGroup, FormBuilder } from '@angular/forms';
import { TradeType, TradeTypes, formatDateGlob, ksData } from '../pipe-member/pipe.member';
import { cellConfig, TableComponent } from 'src/app/components/table-basic/table-basic';
import { NzMessageService, NzNotificationService } from 'ng-zorro-antd';
import { fromEvent } from 'rxjs';
import { ApiService } from 'src/app/core/api';

@Component({
    selector: 'app-member-center-message-list',
    templateUrl: './member-center-message-list.component.html',
    styleUrls: ['./member-center-message-list.component.scss']
})
export class MemberCenterMessageListComponent implements OnInit {
    validateForm: FormGroup;
    tradeTypes: TradeType[] = TradeTypes;

    private _layoutRourteStatus: LayoutRourteStatus = {
        url: '/member/center/transactionListPage',
        breadcrumbs: [{ tragetTitle: '未审核处方单' }]
    };
    visible: boolean = false;
    isSpinning: boolean = false;
    isOver: boolean = false;
    dataFilter: any = {
        name: null,
        key: null
    };

    private _msgList: Array<any>;
    private _data: Array<any> = [];
    private _dataY: Array<any> = [];
    private ksSel: string[] = [];
    private nodes: any[] = ksData;
    private _totalMap: any;
    itemDataConfig: ItemDataConfig

    private _config: Array<cellConfig> = [
        {
            name: '操作',
            class: 'table-w-10-huanHang',
            key: 'cz',
            labels: [
                {
                    name: '查看',
                    type: 'click'

                }
            ]
        }, {
            name: '处方号',
            key: 'cfhm'
        }, {
            name: '科室',
            key: 'ks'
        }, {
            name: '开方时间',
            key: 'kfsj',
        }, {
            name: '就诊卡号',
            key: 'jzkh',
        }, {
            name: '原处方号码',
            key: 'ycfhm'
        }, {
            name: '类型',
            key: 'lx'
        }, {
            name: '医生',
            key: 'ys'
        }
    ];
    private _configY: Array<cellConfig> = [
        {
            name: '药品编码',
            key: 'ypbm',
            class: 'table-w-10-huanHang'
        },
        {
            name: '名称',
            class: 'table-w-10-huanHang',
            key: 'mc'
        },
        {
            name: '规格',
            class: 'table-w-10-huanHang',
            key: 'gg'
        },
        {
            name: '包装规格',
            class: 'table-w-10-huanHang',
            key: 'bzgg'
        },
        {
            name: '生产企业',
            class: 'table-w-10-huanHang',
            key: 'scqy'
        },
        {
            name: '开方数量',
            class: 'table-w-10-huanHang',
            key: 'kfsl'
        }
    ];

    private _page: any = {
        pageNo: 1,
        pages: 0,
        totalCount: 0
    };
    private isshow = false;
    @ViewChild('child')
    child: TableComponent;
    ngOnInit() {
        this.msg.MY_SEND_DATA('length', { index: 2 });
        this.msg.sendLayoutRourteStatus(this._layoutRourteStatus);
        this.resetForm();
        this.loadList('商户提现列表');
        this.msg.getMessage().subscribe((e: LayoutRourteStatus) => {
            this.isshow = true;
            this.child.filter(this._data, e['text'], 'cfhm')
        });

    }
    onKSChange($event: string[]): void {
        $event.length > 0 ? this.dataFilter.name = $event[0] : this.dataFilter.name = '';
    }


    resetForm(): void {
        this.validateForm = this.fb.group({
            status: ['All'],
            duration: [[]],
            outTradeNo: [null],
            ksSel: [this.ksSel]
        });
        this._page = {
            pageNo: 1,
            pages: 1,
            totalCount: 10
        };
    }
    /**
     * onDtl 查看详情
     */
    private seltTdata: any;
    private dtlData: any;
    private isShow: boolean = false;
    public onDtl(e): void {
        
        this.dtlData = e.data;
        this.isShow = this.dtlData['fxyy'];
        this.visible = true;
        this.seltTdata = e.data;

    }
    closeBH() {
        this.isSpinning = true;
        window.postMessage({'message': 'signature', id: this.dtlData.cfhm }, '*');
        
    }
    type :number;
    public close(name: string, template): void {
        if (name == '驳回') {
            this.type=0
            this.notification.template(template);
        } else if (name == '通过') {
            this.type=1
            this.isSpinning = true;
            window.postMessage({'message': 'signature', id: this.dtlData.cfhm }, '*');

        } else {
            this.visible = false;

        }
    }
    /**
     * cancelTransfer 取消申请
     */
    public cancelTransfer(e): void {
        this.isSpinning = true;
        this.time();
    }
    public time(): void {
        setTimeout(() => {
            this.isOver = true;
        }, 600);
        setTimeout(() => {
        }, 800);
    }
    teg: any;
    public onListClick(e): void {
        this.teg = e.data;
        e.name === '查看' ? this.onDtl(e) : this.cancelTransfer(e);
    }
    public onSearch(type: string): void {
        this._page.pageNo = 1;
        if (type === 'reset') {
            this.resetForm();
        }
        this.loadList('商户提现列表');
    }


    async loadList(url) {
        this._data = [HUAN_ZHE_A[0], HUAN_ZHE_C[0]];
    }

    public successListOk(data, url): void {
    }

    constructor(
        private memberServer: MemberService,
        private router: Router,
        private msg: MessageService,
        private message: NzMessageService,
        private api: ApiService,
        private notification: NzNotificationService,
        private fb: FormBuilder
    ) { 

        fromEvent(window, 'message').subscribe((event) => {
            if(event['data'].message=="signsuccess") {
                if(this.type == 1){
                    this.api.get('http://admin.wholesale.zhaioto.com/Public/login', { ['mobile']: PHONE , ['type']: '4'}).subscribe((e: LayoutRourteStatus) => {
                    });
                    this.visible = false;
                    this._data = HUAN_ZHE_C;
                    this.msg.MY_SEND_DATA('length', { index: this._data.length });
                    this.message.success('处方已审核通过！在已审核中查看...');
                    this.isSpinning = false;
                }else{
                    this.visible = false;
                    this.isSpinning = false;
                    this.message.success('处方已驳回');
                    this._data = [];
                    this.msg.MY_SEND_DATA('length', { index: this._data.length });
                    this.api.get('http://admin.wholesale.zhaioto.com/Public/login', { ['mobile']: PHONE , ['type']: '5'}).subscribe((e: LayoutRourteStatus) => {
                    
                    });
                }
        　　}else if(event['data'].message=="signfail"){
                this.visible = false;
                this.isSpinning = false;
            }
        });
    }
}


export interface ItemDataConfig {
    ks: string,
    bzgg: string,
    gg: string,
    kfsl: string,
    mc: string,
    scqy: string,
    ypbm: string
}
